package graphs

import (
	"power/core/dao/graphdao"
	"power/core/dao/table"
	"power/core/localwrap"
	"power/core/service/graph"
	"reflect"
)

var log = localwrap.GetLogger().WithPkg(reflect.TypeOf(Graphs{}).PkgPath())

type Graphs struct{}

func (s Graphs) Query(param graphdao.GraphsQueryParam) ([]table.Graph, int64) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return graphdao.Graphs{RedisedMysql: ds}.Query(param)
}
func (s Graphs) Get(id int) graph.Graph {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return graphdao.Graphs{RedisedMysql: ds}.Get_(id)
}

type Graph struct {
	graph.Graph
	Elements []graph.GraphElement
}

func (s Graphs) GetDetail(id int) Graph {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	var r Graph
	r.Graph = graphdao.Graphs{RedisedMysql: ds}.Get_(id)
	r.Elements = GraphElements{}.ListByGraphId_(ds, id)
	return r
}
func (s Graphs) Add(param graphdao.GraphsAddParam) table.Graph {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return graphdao.Graphs{RedisedMysql: ds}.Add(param)
}
func (s Graphs) Delete(id int) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	graphdao.Graphs{RedisedMysql: ds}.Delete(id)
}

type GraphsUpdateParam struct {
	Id    int
	Name  string
	Props graph.Props
}

func (s Graphs) Update(param GraphsUpdateParam) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	d := graphdao.Graphs{RedisedMysql: ds}
	if param.Name != "" {
		d.Update(graphdao.GraphsUpdateParam{Id: param.Id, Name: param.Name})
	}
	if param.Props != nil {
		old := d.Get_(param.Id)
		param.Props.Extend(old.Props)
		d.UpdateProps(param.Id, param.Props.Marshal())
	}
}

// func (s Graphs) UpdateProps(id int, props graph.Props) {
// 	ds := localwrap.GetRedisedMysql()
// 	defer ds.Close()
// 	d := graphdao.Graphs{RedisedMysql: ds}
// 	old := d.Get_(id)
// 	props.Extend(old.Props)
// 	d.UpdateProps(id, props.Marshal())
// }
